package com.globalresources.ui.struts.action.document;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.globalresources.business.inface.IDocumentService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.PaginationConstant;
import com.globalresources.po.Document;
import com.globalresources.ui.struts.action.document.vo.PatientDocument;

public class PatientDocumentAction extends AbstractAction implements ServletRequestAware {
	
	private List<PatientDocument> patientDocuments ;
	private List<Document> documents ;
	private HttpServletRequest request ;
	private IDocumentService documentService ;
	private Document document ;
	private PatientDocument patientDocument ;
	
	public PatientDocument getPatientDocument() {
		return patientDocument;
	}
	public void setPatientDocument(PatientDocument patientDocument) {
		this.patientDocument = patientDocument;
	}
	public Document getDocument() {
		return document;
	}
	public void setDocument(Document document) {
		this.document = document;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request ;
	}
	public void setDocumentService(IDocumentService documentService) {
		this.documentService = documentService;
	}
	
	public String fetchDocumentByPatient() {
		String caseNumber = request.getParameter("caseNumber");
		String currentPageStr = request.getParameter("pageIndex");
		int currentPage = 1 ;
		try {
			currentPage = Integer.parseInt(currentPageStr);
		} catch (Exception ex) {
			currentPage = 1 ;
		}
		request.setAttribute("pageIndex", currentPage);
		int items = documentService.getDocumentNumByPatient(caseNumber);
		int pageCount = 0 ;
		if ( items % PaginationConstant.PAGESIZE == 0 ) {
			pageCount = items / PaginationConstant.PAGESIZE ;
		} else {
			pageCount = items / PaginationConstant.PAGESIZE  + 1 ;
		}
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("caseNumber",caseNumber);
		documents = documentService.getDocumentByPatient(caseNumber, currentPage, PaginationConstant.PAGESIZE);
		return SUCCESS ;
	}
	public String fetchSignDocumentByPatient() {
		String caseNumber = request.getParameter("caseNumber");
		String currentPageStr = request.getParameter("pageIndex");
		String signStr = request.getParameter("isSign");
		int currentPage = 1 ;
		byte isSign = 1 ;
		try {
			currentPage = Integer.parseInt(currentPageStr);
		} catch ( Exception e) {
			currentPage = 1 ;
		}
		try {
			isSign = Byte.parseByte(signStr);
		} catch ( Exception e) {
			isSign = 1 ;
		}
		request.setAttribute("pageIndex", currentPage);
		request.setAttribute("isSign", isSign);
		int pageCount = 0 ;
		int items = documentService.getSignDocumentNumByPatient(caseNumber, isSign);
		if ( items % PaginationConstant.PAGESIZE == 0 ) {
			pageCount = items/PaginationConstant.PAGESIZE ;
		} else {
			pageCount = items/PaginationConstant.PAGESIZE + 1;
		}
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("caseNumber", caseNumber);
		documents = documentService.getSignDocumentByPatient(caseNumber, isSign, currentPage, PaginationConstant.PAGESIZE);
		return SUCCESS ;
	}
	
	public String fetchPatientDocumentNumber() {
		String currentPageStr = request.getParameter("pageIndex");
		int currentPage = 1 ;
		try {
			currentPage = Integer.parseInt(currentPageStr);
		} catch (Exception e) {
			currentPage = 1 ;
		}
		patientDocuments = documentService.getPatient(currentPage, PaginationConstant.PAGESIZE);
		int pageCount = 1 ;
		int items = documentService.getPatientNum();
		if ( items % PaginationConstant.PAGESIZE == 0 ) {
			pageCount = items / PaginationConstant.PAGESIZE ;
		} else {
			pageCount = items / PaginationConstant.PAGESIZE + 1 ;
		}
		request.setAttribute("pageIndex", currentPage);
		request.setAttribute("pageCount", pageCount);
		return SUCCESS ;
	}
	
	public String searchDocument() {
		String currentPageStr = request.getParameter("pageIndex");
		int currentPage = 1 ;
		try {
			currentPage = Integer.parseInt(currentPageStr);
		} catch (Exception e) {
			currentPage = 1 ;
		}
		String patientName = request.getParameter("patientName");
		String familyName = request.getParameter("familyName");
		String caseNumber = request.getParameter("caseNumber");
		String createTime = request.getParameter("createTime");
		String operateTime = request.getParameter("operateTime");
		String signTime = request.getParameter("signTime");
		String isSign = request.getParameter("isSign");
		String isView = request.getParameter("isView");
		StringBuffer parameters = new StringBuffer();
		patientDocument = new PatientDocument();
		if ( null != patientName && !"".equals(patientName)) {
			patientDocument.setPatientName(patientName);
			parameters.append("&patientName=" + patientName);
		}
		if ( null != familyName && !"".equals(familyName)) {
			patientDocument.setFamilyName(familyName);
			parameters.append("&patientName=" + patientName);
		}
		if ( null != caseNumber && !"".equals(caseNumber)) {
			patientDocument.setCaseNumber(caseNumber);
			parameters.append("&caseNumber=" + caseNumber);
		}
		if ( null != createTime && !"".equals(createTime)) {
			patientDocument.setCreateTime(createTime);
			parameters.append("&createTime=" + createTime);
		}
		if ( null != operateTime && !"".equals(operateTime)) {
			patientDocument.setReceiveTime(operateTime);
			parameters.append("&operateTime=" + operateTime);
		}
		if ( null != signTime && !"".equals(signTime)) {
			patientDocument.setSignTime(signTime);
			parameters.append("&signTime=" + signTime);
		}
		if ( null != isSign && !"".equals(isSign)) {
			patientDocument.setIsSign(Byte.parseByte(isSign));
			parameters.append("&isSign=" + isSign);
		}
		if ( null != isView && !"".equals(isView)) {
			patientDocument.setIsView(Byte.parseByte(isView));
			parameters.append("&isView=" + isView);
		}
		parameters.append("&");
		patientDocuments = documentService.getDocumentByCondition(patientDocument, currentPage, PaginationConstant.PAGESIZE);
		int items = documentService.getDocumentNumByCondition(patientDocument);
		int pageCount = 0 ;
		if ( items % PaginationConstant.PAGESIZE == 0 ) {
			pageCount = items / PaginationConstant.PAGESIZE ;
		} else {
			pageCount = items /PaginationConstant.PAGESIZE + 1 ; 
		}
		request.setAttribute("parameters", parameters.toString());
		request.setAttribute("pageIndex", currentPage);
		request.setAttribute("pageCount", pageCount);
		return SUCCESS ;
	}
	
	public List<PatientDocument> getPatientDocuments() {
		return patientDocuments;
	}
	public void setPatientDocuments(List<PatientDocument> patientDocuments) {
		this.patientDocuments = patientDocuments;
	}
	public List<Document> getDocuments() {
		return documents;
	}
	public void setDocuments(List<Document> documents) {
		this.documents = documents;
	}
	
}
